<?php
/**
 * Created by PhpStorm.
 * User: zlkmu
 * Date: 10/31/2017
 * Time: 11:19 AM
 * 用于记录某一个房间麻将过程的类
 */
    class MaJangRoomRecorder{
        //用于记录玩家摸牌结果的数组（只记录最近一次玩家摸牌结果）
        //某一次玩家打牌后的，其它玩家的可能结果组合
        //array(i=>array('playerID' =>array('pai', 'to'=>当前判断的是哪个玩家, 'peng'=>能否碰，'gang'=>能否杠, 'hu'=>能否胡)))
        private $playerDaPaiResult = array();

        //用于记录玩家打牌结果的数组（只记录最近一次玩家打牌的结果)
        //某一次玩家摸牌后的结果
        //array('playerID'=>array('hu'=>true/false, 'gang'=> MING_GANG/AN_GANG/false, 'ting'=>array(i=>array(打什么牌, array(胡的牌))))
        private $playerMoPaiResult = array();

        //某一次玩家打牌后，其它玩家的选择结果，用于判断碰，杠，胡冲突的情况
        //array(playerID, dicision:0,过，1碰，2杠，3胡)
        private $playerDecision = array();

        //玩家结果记录器
        //'playerID'=>(盘数，分数，原因，面向哪个玩家)
        private $playerWinLoseResult = array();

        //玩家步骤记录器
        //'playerID'=>(计步器, 盘数，第几部，动作（摸牌/打牌/碰牌/杠牌/胡牌), pai)
        private $playerStepRecorder;



        //用于记录整体结果的类，分别为，第几局，来自哪个玩家，支付给哪个玩家，几番，胡牌还是点杠
        private $record = array(/*'pan'=>'', 'fromPlayer'=>'', 'toPlayer'=>'', 'fanshu'=>'', 'type'=>''*/);

        /**
         * 初始化记录器的函数，每盘开始时候调用
         * @param $playerList
         */
        public function initRecorder($playerList){
            for($i = 0; $i < count($playerList); $i++){
                array_push($this->playerDaPaiResult, array($playerList[$i]->getPlayerID()=>''));
                array_push($this->playerMoPaiResult, array($playerList[$i]->getPlayerID()=>''));

            }
        }

        /**
         * 记录某个玩家摸牌的结果
         * @param $playerID
         * @param $moPaiResult
         */
        public function recordMoPaiResult($playerID, $moPaiResult){
            $this->playerMoPaiResult[$playerID] = $moPaiResult;
        }

        /**
         * 记录某个玩家打牌的结果
         * @param $playerID
         * @param $daPaiResult
         */
        public function recordDaPaiResult($playerID, $daPaiResult){
            $this->playerDaPaiResult[$playerID] = $daPaiResult;
            $this->playerDecision = array();
            print_r($this->playerDaPaiResult[$playerID]);
            for($i = 0; $i < count($this->playerDaPaiResult[$playerID]); $i++){
                array_push($this->playerDecision, $this->playerDaPaiResult[$playerID][$i]['to']);
            }

        }

        /**
         * 记录某个玩家某一次选择的结果
         * @param $playerID
         * @param $decision
         */
        public function recordPlayerDecision($playerID, $decision){
            $this->playerDecision[$playerID] = $decision;

        }

        public function recordHuPai($pan, $fromPlayer, $toPlayer, $fanshu, $type){
            array_push($record, array('pan'=>$pan, 'fromPlayer'=>$fromPlayer, 'toPlayer'=>$toPlayer, 'fanshu'=>$fanshu, 'type'=>$type));
        }

        public function recordGangPai($pan, $toPlayer, $toPlayer, $fanshu, $type){

        }

        /**
         * 判断某个玩家能不能碰或者杠
         * @param $playerID 玩家ID
         * @return int  50:数据错误，51:还要等待玩家选择, 52:有玩家已经决定胡牌，不能碰, 53:能够碰
         */
        function canPlayerPengOrGang($playerID){

            if(count($this->playerDaPaiResult) == 0){
                return DATA_ERROR;
            }
            $canPengOrGang = true;
            //记录胡牌玩家数
            $huCount = 0;
            for($i = 0; $i < count($this->playerDaPaiResult); $i++){
                if(($this->playerDaPaiResult[$i]['to'] != $playerID) && ($this->playerDaPaiResult[$i]['hu'])){
                    $canPengOrGang = false;
                    $huCount++;
                }
            }

            //如果有玩家能胡，则要看该玩家是否选择
            if(!$canPengOrGang){

                //如果还没有玩家选择，等待玩家选择
                if(count($this->playerDecision) < $huCount){
                    return WAIT_OTHER_PLAYER;
                }
                else{
                    for($i = 0; $i < count($this->playerDecision); $i++){
                        if(($this->playerDecision[$i]['playerID'] != $playerID) &&
                            ($this->playerDecision[$i]['decision'] == PLAYER_HUPAI)){
                            return OTHER_PLAYER_HU;
                        }
                    }
                    return CAN_PENG_OR_GANG;
                }
            }
            else{
                return CAN_PENG_OR_GANG;
            }
        }




    }



?>